//
//  StatusCell.m
//  OC-LBWeibo
//
//  Created by 李博 on 15/7/14.
//  Copyright © 2015年 libo. All rights reserved.
//

#import "StatusCell.h"
#import "UIImageView+WebCache.h"
#import "Status.h"
#import "StatusPictureCell.h"

#define LBPictureCellReuseIdentifier @"LBPictureCellReuseIdentifier"

@interface StatusCell()<UICollectionViewDataSource>

@property(nonatomic,weak)UIImageView *iconView;
@property (nonatomic, weak)UILabel *nameLabel;
@property (nonatomic, weak)UILabel *timeLabel;
@property (nonatomic, weak)UIImageView *memberIconView;
@property (nonatomic, weak)UIImageView *vipIconView;
@property (nonatomic, weak)UILabel *sourceLabel;
@property (nonatomic, weak)UILabel *contentLabel;

@property(nonatomic,strong)UICollectionViewFlowLayout *pictureLayout;
@property(nonatomic,strong)UICollectionView *pictureView;

@end

@implementation StatusCell

- (void)setStatus:(Status *)status
{
    _status = status;
    
    //设置头像
    [self.iconView sd_setImageWithURL:status.user.profileUrl];
    //设置认证图标
    self.vipIconView.image = status.user.verifiedImage;
    //设置会员图标
    self.memberIconView.image = status.user.mbrankImage;
    //用户昵称
    self.nameLabel.text = status.user.name;
    //发布微博时间
    self.timeLabel.text = @"刚刚";
    
    self.sourceLabel.text = @"来自新浪";
    self.contentLabel.text = status.text;
    
}

- (nonnull instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(nullable NSString *)reuseIdentifier
{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        
        [self addSubview:self.pictureView];
        
        self.iconView.translatesAutoresizingMaskIntoConstraints = NO;
        [self addConstraint:[NSLayoutConstraint constraintWithItem:self.iconView attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeLeft multiplier:1.0 constant:12]];
        [self addConstraint:[NSLayoutConstraint constraintWithItem:self.iconView attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeTop multiplier:1.0 constant:12]];
        [self addConstraint:[NSLayoutConstraint constraintWithItem:self.iconView attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0 constant:34]];
        [self addConstraint:[NSLayoutConstraint constraintWithItem:self.iconView attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0 constant:34]];
        
        self.nameLabel.translatesAutoresizingMaskIntoConstraints = NO;
        [self addConstraint:[NSLayoutConstraint constraintWithItem:self.nameLabel attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:self.iconView attribute:NSLayoutAttributeRight multiplier:1.0 constant:12]];
        [self addConstraint:[NSLayoutConstraint constraintWithItem:self.nameLabel attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.iconView attribute:NSLayoutAttributeTop multiplier:1.0 constant:0]];
        
        self.memberIconView.translatesAutoresizingMaskIntoConstraints = NO;
        [self addConstraint:[NSLayoutConstraint constraintWithItem:self.memberIconView attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:self.nameLabel attribute:NSLayoutAttributeRight multiplier:1.0 constant:12]];
        [self addConstraint:[NSLayoutConstraint constraintWithItem:self.memberIconView attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.nameLabel attribute:NSLayoutAttributeTop multiplier:1.0 constant:0]];
        self.vipIconView.translatesAutoresizingMaskIntoConstraints = NO;
        [self addConstraint:[NSLayoutConstraint constraintWithItem:self.vipIconView attribute:NSLayoutAttributeRight relatedBy:NSLayoutRelationEqual toItem:self.iconView attribute:NSLayoutAttributeRight multiplier:1.0 constant:5]];
        [self addConstraint:[NSLayoutConstraint constraintWithItem:self.vipIconView attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:self.iconView attribute:NSLayoutAttributeBottom multiplier:1.0 constant:5]];
        
        self.timeLabel.translatesAutoresizingMaskIntoConstraints = NO;
        [self addConstraint:[NSLayoutConstraint constraintWithItem:self.timeLabel attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:self.iconView attribute:NSLayoutAttributeRight multiplier:1.0 constant:12]];
        [self addConstraint:[NSLayoutConstraint constraintWithItem:self.timeLabel attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:self.iconView attribute:NSLayoutAttributeBottom multiplier:1.0 constant:0]];
        
        self.sourceLabel.translatesAutoresizingMaskIntoConstraints = NO;
        [self addConstraint:[NSLayoutConstraint constraintWithItem:self.sourceLabel attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:self.timeLabel attribute:NSLayoutAttributeRight multiplier:1.0 constant:12]];
        [self addConstraint:[NSLayoutConstraint constraintWithItem:self.sourceLabel attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:self.timeLabel attribute:NSLayoutAttributeBottom multiplier:1.0 constant:0]];
        
        self.contentLabel.translatesAutoresizingMaskIntoConstraints = NO;
        [self addConstraint:[NSLayoutConstraint constraintWithItem:self.contentLabel attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:self.iconView attribute:NSLayoutAttributeLeft multiplier:1.0 constant:0]];
        [self addConstraint:[NSLayoutConstraint constraintWithItem:self.contentLabel attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.iconView attribute:NSLayoutAttributeBottom multiplier:1.0 constant:12]];
        
        self.pictureView.translatesAutoresizingMaskIntoConstraints = NO;
        [self addConstraint:[NSLayoutConstraint constraintWithItem:self.pictureView attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:self.contentLabel attribute:NSLayoutAttributeLeft multiplier:1.0 constant:0]];
        [self addConstraint:[NSLayoutConstraint constraintWithItem:self.pictureView attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.contentLabel attribute:NSLayoutAttributeBottom multiplier:1.0 constant:12]];
        [self addConstraint:[NSLayoutConstraint constraintWithItem:self.pictureView attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0 constant:290]];
        [self addConstraint:[NSLayoutConstraint constraintWithItem:self.pictureView attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0 constant:90]];
        
    }
    
    return self;
}

//MARK: -------Collection数据源---------
- (NSInteger)collectionView:(nonnull UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.status.pictureURLs.count;
}
- (nonnull UICollectionViewCell *)collectionView:(nonnull UICollectionView *)collectionView cellForItemAtIndexPath:(nonnull NSIndexPath *)indexPath
{
    StatusPictureCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:LBPictureCellReuseIdentifier forIndexPath:indexPath];
    cell.imageURL = self.status.pictureURLs[indexPath.item];
    
    return cell;
}

//MARK: ------懒加载------
- (UIImageView *)iconView
{
    if (_iconView == nil) {
        UIImageView *iconView = [[UIImageView alloc]init];
        [self addSubview:iconView];
        _iconView = iconView;
    }
    return _iconView;
}
- (UILabel *)nameLabel
{
    if (_nameLabel == nil) {
        UILabel *nameLabel = [[UILabel alloc]init];
        nameLabel.font = [UIFont systemFontOfSize:14];
        nameLabel.textColor = [UIColor darkGrayColor];
        [self addSubview:nameLabel];
        _nameLabel = nameLabel;
    }
    return _nameLabel;
}
- (UILabel *)timeLabel
{
    if (_timeLabel == nil) {
        UILabel *timeLabel = [[UILabel alloc]init];
        timeLabel.font = [UIFont systemFontOfSize:10];
        timeLabel.textColor = [UIColor orangeColor];
        [self addSubview:timeLabel];
        _timeLabel = timeLabel;
    }
    return _timeLabel;
}
- (UIImageView *)memberIconView
{
    if (_memberIconView == nil) {
        UIImageView *mIcon = [[UIImageView alloc]init];
        [self addSubview:mIcon];
        _memberIconView = mIcon;
    }
    return _memberIconView;
}
- (UIImageView *)vipIconView
{
    if (_vipIconView == nil) {
        UIImageView *vipIcon = [[UIImageView alloc]init];
        [self addSubview:vipIcon];
        _vipIconView = vipIcon;
        
    }
    return _vipIconView;
}
- (UILabel *)sourceLabel
{
    if (_sourceLabel == nil) {
        UILabel *sourceLabel = [[UILabel alloc]init];
        sourceLabel.textColor = [UIColor darkGrayColor];
        sourceLabel.font = [UIFont systemFontOfSize:10];
        [self addSubview:sourceLabel];
        _sourceLabel = sourceLabel;
    }
    return _sourceLabel;
}
- (UILabel *)contentLabel
{
    if (_contentLabel == nil) {
        UILabel *contentLabel = [[UILabel alloc]init];
        contentLabel.textColor = [UIColor darkGrayColor];
        contentLabel.font = [UIFont systemFontOfSize:15];
        contentLabel.preferredMaxLayoutWidth = [UIScreen mainScreen].bounds.size.width - 16;
        contentLabel.numberOfLines = 0;
        [self addSubview:contentLabel];
        _contentLabel = contentLabel;
    }
    return _contentLabel;
}

- (UICollectionViewFlowLayout *)pictureLayout
{
    if (_pictureLayout == nil) {
        _pictureLayout = [[UICollectionViewFlowLayout alloc]init];
        _pictureLayout.minimumInteritemSpacing = 10;
        _pictureLayout.minimumLineSpacing = 10;
    }
    return _pictureLayout;
}

- (UICollectionView *)pictureView
{
    if (_pictureView == nil) {
        _pictureView = [[UICollectionView alloc]initWithFrame:CGRectZero collectionViewLayout:self.pictureLayout];
        _pictureView.backgroundColor = [UIColor lightGrayColor];
        _pictureView.dataSource = self;
        //注册可重用cell
        [_pictureView registerClass:[StatusPictureCell self] forCellWithReuseIdentifier:LBPictureCellReuseIdentifier];
        
    }
    return _pictureView;
}

@end

